using System;

namespace RockHopper.Common.Model
{
    public class RaceDistance
    {
        public RaceDistance(double distance, UnitType units, string display = null)
        {
            Distance = distance;
            Units = units;
            Display = display ?? Distance + " " + units.FromUnitType(distance);
            HasDisplayOverride = display != null;
        }

        public string Display { get; private set; }
        public bool HasDisplayOverride { get; private set; }
        public double Distance { get; private set; }
        public UnitType Units { get; private set; }

        public static RaceDistance Create(string value)
        {
            var parts = value.Split(':');
            var distance = double.Parse(parts[0]);
            var units = (UnitType)Enum.Parse(typeof(UnitType), parts[1], true);
            var display = parts.Length == 3 ? parts[2] : null;
            return new RaceDistance(distance, units, display);
        }

        public override string ToString()
        {
            return Display;
        }
    }
}